/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License.You may obtain a copy of the License at
	http://www.mozilla.org/MPL/MPL-1.1.html
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import org.aswing.JTextArea;
import org.aswing.UIManager;

import com.bourre.commands.Delegate;
import com.bourre.events.EventBroadcaster;
import com.bourre.events.EventType;

import fever.events.AsFunctionEvent;
import fever.utils.Stringifier;

/**
 * <strong>FvTextArea</strong> component extends AsWing <strong>JTextArea</strong> 
 * adding <strong>Html AsFunction</strong> managment.
 * 
 * <p>Asfunction call are supported like Event API.
 * 
 * <p>Example
 *   var txt : String = "<p>" +
 *   "<a href='asfunction:onMyEvent,1,2,3'>call 1</a> " +
 *   "AND " +
 *   "<a href='asfunction:call,2'>call 2</a>" +
 *   "</p>";
 *   
 *   var label : FvTextArea = new FvTextArea( txt );
 *   
 *   //Registers default asfunction 'call' method.
 *   label.addAsFunctionListener( FvTextArea.onAsFunctionEVENT, Delegate.create( this, _onClicked ) );
 *   
 *   //Registers custom 'onMyEvent' asfunction call.
 *   label.addAsFunctionListener( 'onMyEvent', this, _onMyFunction );
 *   
 *   
 *   
 *   private function _onClicked( event : AsFunctionEvent ) : Void 
 *   {
 *   	FeverDebug.INFO( event.getType() + ' with ' + event.getArguments() );
 *   }
 * 
 * @see fever.events.AsFunctionEvent
 * 
 * @author Romain Ecarnot
 */
class fvaswing.components.FvTextArea extends JTextArea
{
	//-------------------------------------------------------------------------
	// Events definition
	//-------------------------------------------------------------------------
	
	/** Broadcasted when a <code>asfunction</code> is called. */
	public static var onAsFunctionEVENT : EventType = AsFunctionEvent.onAsFunctionEVENT;
	
	
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _bIsLabelModeEnabled : Boolean;
	private var _oEB : EventBroadcaster;
	private var _aBuffer:Array;
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * FvTextArea( text : String, rows : Number, columns : Number)<br>
	 * FvTextArea( text : String, rows : Number) columns default to 0<br> 
	 * FvTextArea( text : String) rows and columns default to 0<br>
	 * FvTextArea() text default to 0, rows and columns default to 0<br>
	 */
	public function FvTextArea( text : String, rows : Number, columns : Number  ) 
	{
		super( text, rows, columns );
		
		_aBuffer = new Array();
		_oEB = new EventBroadcaster( this );
		
		_bIsLabelModeEnabled = false;
		
		addEventListener( JTextArea.ON_CREATED, _initialize, this );	
	}
	
	/**
	 * Transforms text area into a simple label zone if passed-in 
	 * <code>b</code> is <code>true</code>
	 * 
	 * <p>No border, no background and no focus.
	 */
	public function setLabelModeEnabled( b : Boolean ) : Void
	{
		if( b == _bIsLabelModeEnabled ) return;
		
		if( b )
		{
			setBorder( null );
			setEnabled( false );
			setFocusable( false );
			setOpaque( false );
			setEditable( false );
		}
		else
		{
			setBorder( UIManager.getBorder( 'TextArea.border' ) );
			setEnabled( true );
			setFocusable( true );
			setOpaque( true );
			setEditable( true );
		}
		
		_bIsLabelModeEnabled = b;
	}
	
	/**
	 * Returns <code>true</code> if area is in <strong>label</strong> mode.
	 * 
	 * @return Boolean
	 */
	public function isLabelModeEnabled(  ) : Boolean
	{
		return _bIsLabelModeEnabled;
	}
	
	/**
	 * Adds passed-in {@code listener} listener for receiving 
	 * passed-in {@code asFunctionName} <code>asfunction</code> call.
	 * 
	 * @param asFunctionName Name of the called asfunction.
	 * @param listener Listener object.
	 */
	public function addAsFunctionListener( asFunctionName : String, listener ) : Void
	{
		if( getTextField() )_registerCustomAsFunction.apply( this, arguments );
		else _aBuffer.push( arguments );
	}
	
	/**
	 * Removes passed-in {@code listener} listener that suscribed 
	 * passed-in {@code asFunctionName} <code>asfunction</code> call.
	 * 
	 * @param asFunctionName Name of the called asfunction.
	 * @param listener Listener object.
	 */
	public function removeAsFunctionListener( asFunctionName, listener ) : Void
	{
		_oEB.removeEventListener( asFunctionName, listener );
		
		if( !_oEB.listenerArrayExists( asFunctionName ) )
		{
			delete textField._parent[ asFunctionName ];
		}
	}
	
	/**
	 * Returns string representation.
	 */
	public function toString() : String
	{
		return Stringifier.parse( this );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	private function _initialize( target : FvTextArea ) : Void 
	{
		var l : Number = _aBuffer.length;
		for ( var i : Number = 0; i < l; i++ ) _registerCustomAsFunction.apply( this, _aBuffer[i] );
		
		_aBuffer = new Array();
	}
	
	private function _registerCustomAsFunction( asFunctionName : String ) : Void
	{
		_oEB.addEventListener.apply( _oEB, arguments );	
		
		if( !textField._parent[ asFunctionName ] )
		{
			textField._parent[ asFunctionName ] = Delegate.create( this, _disptachCustomEvent, asFunctionName );	
		}
	}
	
	private function _disptachCustomEvent( ) : Void 
	{
		var eventName : String = String( arguments.pop() );
		var e : AsFunctionEvent = new AsFunctionEvent( eventName, this, arguments );
		
		_oEB.broadcastEvent( e );
	}
	
}